package com.ldf.jrtsp.utils;

import com.ldf.jrtsp.exception.JRtspException;

import java.security.MessageDigest;

/**
 * md5工具
 *
 * @author lidaofu
 * @since 2024/6/19
 **/
public class MD5Util {
    /**
     * Md5加密
     *
     * @param str
     * @return
     */
    public static String encrypt(String str) {
        StringBuilder sb = new StringBuilder();
        try {
            byte[] digest = null;
            try {
                MessageDigest md5 = MessageDigest.getInstance("md5");
                digest = md5.digest(str.getBytes("utf-8"));
            } catch (Exception e) {
                e.printStackTrace();
            }
            for (byte b : digest) {
                int c = b & 0xFF;
                if (c < 16) {
                    sb.append("0");
                }
                sb.append(Integer.toHexString(c));
            }
            return sb.toString();
        } catch (Exception e) {
            throw new JRtspException("加密失败", e);
        }
    }


}
